package com.stone.base.interceptor;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

/**
 * 登陆拦截器
 * @author Stone P
 *
 */
public class LoginInterceptor extends HandlerInterceptorAdapter {
	private List<String> excludeUrl;

	public void setExcludeUrl(List<String> excludeUrl) {
		this.excludeUrl = excludeUrl;
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		boolean check = true;
		String requestPath = request.getRequestURI();
		//是否需要过滤
		for (String exclude : excludeUrl) {
			if(requestPath.contains(exclude)) {
				check = false;
				break;
			}
		}
		if(false == check) {
			return true;
		} else {
			//获取当前登陆用户信息
			Object userObj = request.getSession().getAttribute("currentUser");
			if(null == userObj) {
				response.sendRedirect("/BreakingFast/backLogin.jsp");
				return false;
			}
			return true;
		}
	}
}
